package com.ksy.map;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.ksy.common.utils.LogTool;

import java.util.ArrayList;
import java.util.List;

public class GDMapView extends TextureMapView {
    private AMap aMap;
    private MarkerClickListener markerClickListener;
    private InfoWindowClickListener infoWindowClickListener;

    public GDMapView(Context context) {
        super(context);
    }

    public GDMapView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public GDMapView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }

    public GDMapView(Context context, AMapOptions aMapOptions) {
        super(context, aMapOptions);
    }

    @Override
    public AMap getMap() {
        return super.getMap();
    }

    public Marker showMarker(LatLng latLng) {
        if (aMap == null) {
            aMap = getMap();
            aMap.setOnInfoWindowClickListener(new AMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
                    LogTool.e("onInfoWindowClick    " + marker.getObject());
                }
            });
            aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
//                    if (marker.isInfoWindowShown()) {
//                        marker.hideInfoWindow();
//                    } else {
//                        marker.showInfoWindow();
//                    }
                    return true;
                }
            });
            InfoWinDow infoWinDow = new InfoWinDow(getContext());
            aMap.setInfoWindowAdapter(infoWinDow);
        }
        Marker marker = aMap.addMarker(new MarkerOptions().position(latLng)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_icon_marker))
                .anchor(0.5f, 0.5f));
        marker.setClickable(true);
        marker.setObject(latLng.toString());
        marker.setInfoWindowEnable(true);
        if (!marker.isInfoWindowShown()) {
            marker.showInfoWindow();
        }
        return marker;
    }

    public void showMarkers(List<LatLngProxy> list) {
        if (aMap == null) {
            aMap = getMap();
            aMap.setOnInfoWindowClickListener(new AMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
                    LogTool.e("onInfoWindowClick    " + marker.getObject());
                    if (infoWindowClickListener != null) {
                        infoWindowClickListener.onInfoWindowClick(marker);
                    }
                }
            });
            aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
//                    if (marker.isInfoWindowShown()) {
//                        marker.hideInfoWindow();
//                    } else {
//                        marker.showInfoWindow();
//                    }
                    if (markerClickListener != null) {
                        markerClickListener.onMarkerClick(marker);
                    }
                    return true;
                }
            });
            InfoWinDow infoWinDow = new InfoWinDow(getContext());
            aMap.setInfoWindowAdapter(infoWinDow);
        }
        if (list == null) {
            return ;
        }
        for (LatLngProxy latLngProxy : list) {
            Marker marker = aMap.addMarker(new MarkerOptions().position(latLngProxy.position())
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_icon_marker))
                    .anchor(0.5f, 0.5f));
            marker.setClickable(true);
            marker.setObject(latLngProxy.tag());
            marker.setInfoWindowEnable(true);
        }
    }
    public List<Marker> showMarker(List<LatLng> latLngs) {
        List<Marker> markerList = new ArrayList<>();
        if (aMap == null) {
            aMap = getMap();
            aMap.setOnInfoWindowClickListener(new AMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
                    LogTool.e("onInfoWindowClick    " + marker.getObject());
                    if (infoWindowClickListener != null) {
                        infoWindowClickListener.onInfoWindowClick(marker);
                    }
                }
            });
            aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
//                    if (marker.isInfoWindowShown()) {
//                        marker.hideInfoWindow();
//                    } else {
//                        marker.showInfoWindow();
//                    }
                    if (markerClickListener != null) {
                        markerClickListener.onMarkerClick(marker);
                    }
                    return true;
                }
            });
            InfoWinDow infoWinDow = new InfoWinDow(getContext());
            aMap.setInfoWindowAdapter(infoWinDow);
        }
        if (latLngs == null) {
            return markerList;
        }
        for (LatLng latLng : latLngs) {
            Marker marker = aMap.addMarker(new MarkerOptions().position(latLng)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_icon_marker))
                    .anchor(0.5f, 0.5f));
            marker.setClickable(true);
            marker.setObject(latLng.toString());
            marker.setInfoWindowEnable(true);
            markerList.add(marker);
        }
        return markerList;
    }

    public void setMarkerClickListener(MarkerClickListener markerClickListener) {
        this.markerClickListener = markerClickListener;
    }

    public void setInfoWindowClickListener(InfoWindowClickListener infoWindowClickListener) {
        this.infoWindowClickListener = infoWindowClickListener;
    }

    private class InfoWinDow extends LinearLayout implements AMap.InfoWindowAdapter {
        public InfoWinDow(Context context) {
            this(context, null);
        }

        public InfoWinDow(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            LayoutInflater.from(context).inflate(R.layout.map_marker_infowindow, this, true);
            setBackgroundResource(R.drawable.map_icon_location_pop);
        }

        @Override
        public View getInfoWindow(Marker marker) {
            Object object = marker.getObject();
            LogTool.e("Marker   " + object);
            return this;
        }

        @Override
        public View getInfoContents(Marker marker) {
            return null;
        }
    }

    public interface MarkerClickListener {
        void onMarkerClick(Marker marker);
    }

    public interface InfoWindowClickListener {
        void onInfoWindowClick(Marker marker);
    }
}
